package cn.lf.controller;

import cn.lf.constant.ResponseCodeEnum;
import cn.lf.dto.LoginUserInfoDTO;
import cn.lf.dto.system.*;
import cn.lf.service.ISystemService;
import cn.lf.utils.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sys")
@Api(tags = "用户登录相关接口")
public class SystemController {

    @Autowired
    private ISystemService systemService;

    @GetMapping("/login")
    @ApiOperation("登录")
    public ResponseModel<LoginUserInfoDTO> login(@RequestParam("userName") String userName,
                                                 @RequestParam("password") String password){

        LoginUserInfoDTO result = systemService.login(userName,password);

        return new ResponseModel(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),result);
    }

    @PostMapping("/registerUser")
    @ApiOperation("注册用户")
    public ResponseModel<RegisterUserRepDTO> registerUser(@Validated @RequestBody RegisterUserReqDTO registerUserReqDTO){

        RegisterUserRepDTO result = systemService.registerUser(registerUserReqDTO);

        return ResponseModel.buildSuccessResult(result);
    }

    @GetMapping("/judgeSingle")
    @ApiOperation("判断唯一数据是否存在")
    public ResponseModel<JudgeSingleRepDTO> judgeSingle(JudgeSingleReqDTO judgeSingleReqDTO){

        JudgeSingleRepDTO result = systemService.judgeSingle(judgeSingleReqDTO);

        return ResponseModel.buildSuccessResult(result);

    }

    @PostMapping("/changePassword")
    @ApiOperation("变更用户密码")
    public ResponseModel changePassword(@RequestBody PasswordChangeDTO passwordChangeDTO){

        systemService.changePassword(passwordChangeDTO);

        return ResponseModel.buildSuccessResult();
    }

}
